using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using ATest.Bll.Interfaces;
using ATest.Bll.Repositories;
using ATest.Bll;
using ATest.Bll.Errors;

namespace ATest.Controllers
{
    [HandleError]
    [Authorize]
    public class TestController : Controller
    {
        private ITestRepository _testRepository;
        public TestController() : this(new TestRepository())
        {
        }

        public TestController(ITestRepository testRepository)
        {
            _testRepository = testRepository;
        }

        //
        // GET: /Test/
        public ActionResult Index()
        {
            var data = _testRepository.GetAll().ToList();
            if (Request.IsAjaxRequest())
                return PartialView("Ajax_Index", data);
            else
                return View(data);
        }


        //
        // GET: /Test/Details/1
        public ActionResult Details(int id)
        {
            var data = _testRepository.GetById(id);
            if (data != null)
            {
                if(Request.IsAjaxRequest())
                    return PartialView("Ajax_Details", data);
                else
                    return View(data);
            }
            else
            {
                return View("NotFound");
            }
        }

        //
        // GET: /Test/Edit/1
        public ActionResult Edit(int id)
        {
            var data = _testRepository.GetById(id);
            if (data != null)
            {
                if (Request.IsAjaxRequest())
                    return PartialView("Ajax_Edit", data);
                else
                    return View(data);
            }
            else
            {
                return View("NotFound");
            }
        }


        //
        // GET: /Test/Create
        public ActionResult Create()
        {
            if (Request.IsAjaxRequest())
                return PartialView("Ajax_Create");
            else
                return View();
        }


        //
        // POST: /Test/Edit/1
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(int id, string name)
        {
            var tmp = _testRepository.GetById(id);
            if (tmp != null)
            {
                UpdateModel(tmp, new []{"Name"});

                try
                {
                    _testRepository.SaveChanges();
                    if (Request.IsAjaxRequest())
                        return PartialView("Ajax_Index", _testRepository.GetAll().ToList());
                    else
                        return RedirectToAction("Index");
                }
                catch (ValidationException)
                {
                    foreach (var e in tmp.GetRuleViolations())
                    {
                        this.ModelState.AddModelError(e.PropertyName, e.ErrorMessage);
                    }
                    if (Request.IsAjaxRequest())
                        return PartialView("Ajax_Edit", tmp);
                    else
                        return View(tmp);
                }
            }
            else
            {
                return View("NotFound");
            }
        }

        //
        // POST: /Test/Create
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateAntiForgeryToken]
        public ActionResult Create(string name)
        {
            var tmp = new Test();
            UpdateModel(tmp, new[] { "Name" });

            try
            {
                _testRepository.Insert(tmp);
                _testRepository.SaveChanges();
                if (Request.IsAjaxRequest())
                    return PartialView("Ajax_Index", _testRepository.GetAll().ToList());
                else
                    return RedirectToAction("Index");
            }
            catch (ValidationException)
            {
                foreach (var e in tmp.GetRuleViolations())
                {
                    this.ModelState.AddModelError(e.PropertyName, e.ErrorMessage);
                }
                if (Request.IsAjaxRequest())
                    return PartialView("Ajax_Create", tmp);
                else
                    return View(tmp);
            }
        }

        //
        // POST: /Test/Delete/1
        [AcceptVerbs(HttpVerbs.Post)]
        [ValidateAntiForgeryToken]
        public ActionResult Delete(int id)
        {
            var tmp = _testRepository.GetById(id);
            if (tmp != null)
            {
                _testRepository.Delete(tmp);
                _testRepository.SaveChanges();
                if (Request.IsAjaxRequest())
                    return PartialView("Ajax_Index", _testRepository.GetAll().ToList());
                else
                    return RedirectToAction("Index");
            }
            else
            {
                return View("NotFound");
            }
        }
    }
}
